/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

/* eslint-disable react/jsx-no-bind */
/* eslint-disable react/sort-comp */
import React, { Component } from 'react';
import { connect } from 'dva';
import { debounce } from 'lodash';
import AreaList from '@/components/AreaList';
import InfoCheck from '@/components/InfoCheck';
import Search from './Search';
import Pagination from './Pagination';

@connect(({ loading, area, user }) => ({
  user,
  area,
  loading: loading.effects['area/subList'],
}))
export default class Regional extends Component {
  state = {
    current: 1,
    count: 1,
    pageSize: 10,
  };

  getAreaList(page = 1) {
    const {
      dispatch,
      area,
      user: {
        currentUser: { user_info = {} },
      },
    } = this.props;
    const { independent } = area;
    const { area_ids, inn_area_ids = [] } = user_info;
    // 片区初始值
    const pianquReqData =
      inn_area_ids.length > 0 ? { area_id: area_ids == '*' ? 0 : inn_area_ids.join(',') } : {};
    dispatch({
      type: 'area/subList',
      payload: {
        is_district: '1',
        key: '2',
        keyword: independent.keyword ? independent.keyword : '',
        pageNum: page,
        ...pianquReqData,
      },
    });
  }

  handleInfoReady(data) {
    if (!data.name) return;
    this.getAreaList();
  }

  // 分页
  onChange = debounce(
    page => {
      this.getAreaList(page);
      this.setState({
        current: page,
      });
    },
    500,
    {
      leading: true,
      trailing: false,
    },
  );

  render() {
    const { loading, area } = this.props;
    const { current, count, pageSize } = this.state;
    const { independent } = area;
    const { paginations } = independent;
    const currentPage = paginations ? paginations.page : current;
    const totalNum = paginations ? paginations.count : count;
    const pageSizeNum = paginations ? paginations.pageSize : pageSize;
    return (
      <div>
        <InfoCheck onReady={this.handleInfoReady.bind(this)} />
        <Search searchType="2" current={currentPage} />
        <AreaList loading={loading} data={independent} post="2" state={this.state} />
        <Pagination
          current={currentPage * 1}
          count={totalNum}
          onChange={this.onChange}
          pageSize={pageSizeNum}
        />
      </div>
    );
  }
}
